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PV4
At0FG+CpeXBk8c8FtDXKie/ACdrZWpEj66tS6DpQ0bbB9B7xDjIo8LwFVX2GMul9
MVrTZl30AkT68r1+sRl0wOJDAAgPvXdPmd9tx+ECgYBf9nhF+AUtY9qKvIEXQYAR
40irnQc1YPC6s4RLcUNLn6NfofuDMjp5hRrCiSBAT8Ly+5oTIyjYn/nrXCEvP9x6
cbRcoV64Ah9LK7NOmKnjLB3i82Q9Nc0xByl8LkdgPM+Zs2coa+txfKQLkgjWO/Vm
Gp2zC28GnDCceElXnoALcQKBgFNbHpSQwPjcDFQp9y5Q7Ha/QNSa77O9Ojak2cDX
cq7Oape33ly7lJt8oNm+NC1+7Uu/qXvDNWPX6iA3OMVnpZU38Csr05HuZR1Fu3kj
6Yu/Ng+En8LyYz642Qs4kC1hL8trKNVb4h1F1hfs6WZaDBRDZTe4CS5ITVST6WYX
K3zBAoGBALfIGeYZoM4flG2yHTHpU3+nZgoL89Zaq9ZueIu5IK+UvPO+CkHrvo6u
2YWVLQ9uQmydxUmWThXiC47FNwxD5gqDgtawpUPaDykC/KBAIlzkj8WIx2DjVOXT
FAbMctVs6Z+qBoUlmqYwEclqKdtEr4Ok8Tgmvj7YghaideSOt5PY
-----END RSA PRIVATE KEY-----`

/* PKCS8: openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pem */
// const priKey2 = `-----BEGIN PRIVATE KEY-----
// MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7BB2j/fmJOWIt
// AJEGGZIT+vnRhcWwZeQ0bLPTFPEubrQYXeAICglHnBNZOaF7uCZdcp9FnHcx/61P
// EdbXxoFIFrUO5f69kDrXyvrGkhb3eQGYilD1+8EivhJ5LQQo3Fqz2JOcd0oTaRF1
// 0tyYRtufoBFpoeBSxgD7ovHqYwpxV4ZZvHciBZ5ZftF/YKb1kzKpnw/NrdkSZPwY
// bSYhTHjFish2sGx2V6JW9S1xLcNv054ODJQ9Th5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yg2b40LX7tS7+p
// e8M1Y9fqIDc4xWellTfwKyvTke5lHUW7eSPpi782D4SfwvJjPrjZCziQLWEvy2so
// 1VviHUXWF+zpZloMFENlN7gJLkhNVJPpZhcrfMECgYEAt8gZ5hmgzh+UbbIdMelT
// f6dmCgvz1lqr1m54i7kgr5S8874KQeu+jq7ZhZUtD25CbJ3FSZZOFeILjsU3DEPm
// CoOC1rClQ9oPKQL8oEAiXOSPxYjHYONU5dMUBsxy1Wzpn6oGhSWapjARyWop20Sv
// g6TxOCa+PtiCFqJ15I63k9g=
// -----END PRIVATE KEY-----`

/* openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem */
const pubKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuwQdo/35iTliLQCRBhmS
E/r50YXFsGXkNGyz0xTxLm60GF3gCAoJR5wTWTmhe7gmXXKfRZx3Mf+tTxHW18aB
SBa1DuX+vZA618r6xpIW93kBmIpQ9fvBIr4SeS0EKNxas9iTnHdKE2kRddLcmEbb
n6ARaaHgUsYA+6Lx6mMKcVeGWbx3IgWeWX7Rf2Cm9ZMyqZ8Pza3ZEmT8GG0mIUx4
xYrIdrBsdleiVvUtcS3Db9OeDgyUPU4eWjhJ8CppORKWY3fxpHUyGXFyyUCeke8P
V2hY57r5gbFBNmY1tuS75oIScFYBXH2aJu5lY5sR5MtVb8quphd4ST5dNWOSR2qV
UQIDAQAB
-----END PUBLIC KEY-----`

func main() {
	rk, err := utils.NewRsaKeys(pubKey, priKey)
	if err != nil {
		println("---------- init error ------------")
		log.Fatalln(err)
	}
	txt := "abc123"
	println(txt)

	xx, err := rk.EncodeWithPublicKey(txt)
	if err != nil {
		log.Fatalln(err)
	}
	println("---- encoded -------")
	println(xx)

	yy, err := rk.DecodeWithPrivateKey(xx)
	if err != nil {
		log.Fatalln(err)
	}
	println("------- decoded ---------")
	println(yy)

	sign, err := rk.SignWithPrivateKey(txt)
	if err != nil {
		println("----------- sign error -------------")
		println(err.Error())
		return
	}
	println("----- signed -----\n" + sign)
	if err = rk.VerifyWithPublicKey(sign, txt); err == nil {
		println("----- checked ok -----")
		return
	}
	println("----- checked no -----")
}
